package com.base.cn.platform.os.controller.web.course.book;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.count.PageFrom;
import com.base.cn.platform.os.common.utils.count.PageKeyUtils;
import com.base.cn.platform.os.common.utils.order.ShopType;
import com.base.cn.platform.os.service.course.book.BookService;
import com.base.cn.platform.os.service.manage.count.PageViewCountService;
import com.base.cn.platform.os.service.upload.LoggerGenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 前台图片Controller
 *
 * @author s.li
 * @create 2018-05-10-19:38
 */
@Controller
@RequestMapping("/web/book")
public class WebBookController extends WebBaseController {
    @Autowired
    private BookService bookService;
    @Autowired
    private PageViewCountService pageViewCountService;
    @Autowired
    private LoggerGenService loggerGenService;

    /**
     * 初始化图书列表页面
     * @return 列表页面
     */
    @RequestMapping("/list")
    public String bookList(){
        pageViewCountService.addPageCount(PageKeyUtils.BOOK_LIST, PageFrom.PAGE_FROM_PC);
        return resultPage("/course/book/book-list");
    }

    /**
     * 加载图书数据列表
     * @param model
     * @param params
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajaxBookList")
    public String ajaxBookList(Model model,
                               @RequestBody Map<String,Object> params,
                               @RequestParam("currentPage") int currentPage) {
        params.put("status",1);
        Map<String,Object> dataMap = bookService.findWebBookPage(params,currentPage,5,true,false);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/course/book/ajax-book-list");
    }

    /**
     * 图书详情
     * @param model
     * @param bookId 图书ID
     * @return 详情页面
     */
    @RequestMapping("/info")
    public String bookInfo(HttpServletRequest request,Model model,
                           @RequestParam("bookId")BigDecimal bookId){
        Map<String,Object> book = bookService.findBookById(bookId,true,true);
        model.addAttribute("book",book);
        loggerGenService.genGoodsPVUVLog(bookId, ShopType.ORDER_SHOP_BOOK);
        return resultPage("/course/book/book-info");
    }
}
